<?php

require_once 'autoload.php';

$action = $_GET['action'];

if ($action == '' || trim($action) == '')
{
    $action = 'index';
}

if ($action == 'index')
{
    $view = new Core_View("admin/indexperson.html");
    $db = Core_Db::factory();
    $sql = "select * from person";
    $ret = $db->fetchAll($sql);
    $view->persons = $ret;
    echo $view->fetch();
}

if ($action == 'add')
{
    $view = new Core_View('admin/addperson.html');
    $form = new Core_Form_Person($view->getView());
    $form->setAction("admin.person.php?action=addconfirm");
    $form->setMethod("post");
    $view->form = $form;
    echo $view->fetch();
}

if ($action == 'modify')
{
    $view = new Core_View('admin/modifyperson.html');
    $id = $_GET['id'];
    if (Zend_Validate::is($id, 'Int'))
    {
        $sql = "select * from person where p_id = ?";
        $db = Core_Db::factory();
        $sql = $db->quoteInto($sql, $id);
        $ret = $db->fetchRow($sql);
        $form = new Core_Form_Person($view->getView(), $ret);
        $form->setAction("admin.person.php?action=modifyconfirm");
        $person_id_element = new Zend_Form_Element_Hidden("p_id", array('value'=>$id));
        $form->addElement($person_id_element);
        $form->setMethod("post");
        $view->form = $form;
        echo $view->fetch();
    }
    
}

if ($action == 'delete')
{
    
}

//执行写入操作...
if ($action == 'addconfirm')
{
    $bind = array();
    $bind['name'] = $_POST['name'];
    $bind['link'] = $_POST['link'];
    $db = Core_Db::factory();
    $db->insert('person', $bind);
    header("Location: admin.person.php?action=index");
}

if ($action == 'modifyconfirm')
{
    $post = $_POST;
    $bind = array(
        'name' => $post['name'],
        'link' => $post['link']
    );
    $db = Core_Db::factory();
    $where = array();
    $where[] = $db->quoteInto('p_id = ?', $post['p_id']);
    $ret = $db->update('person', $bind , $where);
    header("Location: admin.person.php?action=index");
}
